package com.foo.DataReciever;

/*
* Author: Sowmya Kameswaran
*/
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
//import javax.jms.QueueSession;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;

import com.foo.Entity.TickerInfoData;

public class DataReciever implements MessageListener
{
	TopicConnection connection = null;
	TopicSession session = null;
	TickerInfoData info[]=null;
	int j =0;
	String[] data_to_be_passed=null;
	String retrieved_data="";
	private int r_flag=0;

	public DataReciever()
	{
		try
		{		
			Properties props = new Properties();
			props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			props.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(props);
			TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("XAConnectionFactory");
			connection = factory.createTopicConnection();
			session = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
			Topic topic = (Topic) ctx.lookup("topic/MySpatialTopic");
			Topic snapshottopic = (Topic) ctx.lookup("topic/MySnapshotTopic");
			Topic temporaltopic = (Topic) ctx.lookup("topic/MyTemporalTopic");
			Topic analyzedtopic = (Topic) ctx.lookup("topic/MyAnalyzedTopic");
			Topic historictopic = (Topic) ctx.lookup("topic/MyHistoricData");
			MessageConsumer reciever = session.createConsumer(topic);
			MessageConsumer snapshotreciever1 = session.createConsumer(snapshottopic);
			MessageConsumer temporalreciever1 = session.createConsumer(temporaltopic);
			MessageConsumer analyzedreciever1 = session.createConsumer(analyzedtopic);
			MessageConsumer historicreciever = session.createConsumer(historictopic);
			System.out.println("Client started waiting for Server message \n");
			connection.start();

			reciever.setMessageListener(this);
			snapshotreciever1.setMessageListener(this);
			temporalreciever1.setMessageListener(this);
			analyzedreciever1.setMessageListener(this);
			historicreciever.setMessageListener(this);

		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	@Override
	public void onMessage(Message message) 
	{
		TextMessage data = (TextMessage)message;
		//ObjectMessage data=(ObjectMessage)message;
		retrieved_data = "";
		try 
		{
			System.out.println("Reading message: " + data.getText()); 
			retrieved_data = data.getText();
			parsedata(retrieved_data);	
			r_flag = 1;
		} 
		catch (JMSException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			if(session != null )
				session.close();
			if(connection != null)
			connection.close();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void parsedata(String retrieved_data)
	{
		data_to_be_passed = retrieved_data.split(","); 
		int total_size=data_to_be_passed .length;
		System.out.println("the size of data at client end "+ total_size);
	}
	public String[] readtickerinfodata()
	{
		while(r_flag == 0)
		{
		//go in loop till data is arrived!	
		}
		return data_to_be_passed;
	}

}

